<?php
class TutorInfo extends Controller {
	var $msg = '';
	
	function TutorInfo()
	{
		parent::Controller();
	}

	function index()
	{
	 	if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$data['form_status'] = 'add';
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->view('header');
		$this->load->view('tutor-info-form', $data);
		$this->load->view('footer');
	}
	
	function upload() 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->model('TutorModel');
		$this->load->model('TutorSchoolModel');
		$data = array();
		$title = $this->input->post('radTitle');
		$titleOther = $this->input->post('txtTitleOther');
		if($title != 'อื่นๆ') {
			$data['title'] = $title;
		} else {
			$data['title'] = $titleOther;
		}
		
		$data['username'] = $this->input->post('txtUsername') ? $this->input->post('txtUsername') : null;
		$data['password'] = $this->input->post('txtPassword') ? $this->input->post('txtPassword') : null;
		$data['user_expire'] = $this->input->post('txtPasswdExpire') ? $this->input->post('txtPasswdExpire') : null;
		$data['first_name_th'] = $this->input->post('txtFirstNameTh');
		$data['last_name_th'] = $this->input->post('txtLastNameTh');
		$data['nick_name'] = $this->input->post('txtNickName');
		$data['first_name_en'] = $this->input->post('txtFirstNameEn');
		$data['last_name_en'] = $this->input->post('txtLastNameEn');
		$data['birthday'] = $this->input->post('txtBirthday') ? $this->input->post('txtBirthday') : null;
		$data['citizen_id'] = $this->input->post('txtCitizenId') ? $this->input->post('txtCitizenId') : null;
		$data['comment'] = $this->input->post('txtComment') ? $this->input->post('txtComment') : null;
		$data['isBlacklist'] = $this->input->post('chkBlackList');
		$data['blackList_comment'] = $this->input->post('txtBlackList') ? $this->input->post('txtBlackList') : null;
		
		$data['edu_status'] = $this->input->post('rdEduStatus') ? $this->input->post('rdEduStatus') : null;
		$data['edu_current_level'] = $this->input->post('studyingLevel') ? $this->input->post('studyingLevel') : null;
		$data['edu_current_university'] = $this->input->post('studyingUniversity') ? $this->input->post('studyingUniversity') : null;
		$data['edu_current_department'] = $this->input->post('studyingMajor') ? $this->input->post('studyingMajor') : null;
		$data['edu_current_student_id'] = $this->input->post('studyingID') ? $this->input->post('studyingID') : null;
		$data['edu_current_class'] = $this->input->post('studyingClass') ? $this->input->post('studyingClass') : null;
		$data['edu_current_GPA'] = $this->input->post('studyingGPA') ? $this->input->post('studyingGPA') : null;
		$data['edu_pratom_school'] = $this->input->post('pratomSchool') ? $this->input->post('pratomSchool') : null;
		$data['edu_pratom_major'] = $this->input->post('pratomMajor') ? $this->input->post('pratomMajor') : null;
		$data['edu_pratom_student_id'] = $this->input->post('pratomID') ? $this->input->post('pratomID') : null;
		$data['edu_pratom_GPA'] = $this->input->post('pratomGPA') ? $this->input->post('pratomGPA') : null;
		$data['edu_mo_tom_school'] = $this->input->post('tonSchool') ? $this->input->post('tonSchool') : null;
		$data['edu_mo_tom_major'] = $this->input->post('tonMajor') ? $this->input->post('tonMajor') : null;
		$data['edu_mo_tom_student_id'] = $this->input->post('tonID') ? $this->input->post('tonID') : null;
		$data['edu_mo_tom_GPA'] = $this->input->post('tonGPA') ? $this->input->post('tonGPA') : null;
		$data['edu_mo_pay_school'] = $this->input->post('paySchool') ? $this->input->post('paySchool') : null;
		$data['edu_mo_pay_major'] = $this->input->post('payMajor') ? $this->input->post('payMajor') : null;
		$data['edu_mo_pay_student_id'] = $this->input->post('payID') ? $this->input->post('payID') : null;
		$data['edu_mo_pay_GPA'] = $this->input->post('payGPA') ? $this->input->post('payGPA') : null;
		$data['edu_bachelor_university'] = $this->input->post('bSchool') ? $this->input->post('bSchool') : null;
		$data['edu_bachelor_major'] = $this->input->post('bMajor') ? $this->input->post('bMajor') : null;
		$data['edu_bachelor_student_id'] = $this->input->post('bID') ? $this->input->post('bID') : null;
		$data['edu_bachelor_GPA'] = $this->input->post('bGPA') ? $this->input->post('bGPA') : null;
		
		$data['edu_master_university'] = $this->input->post('mSchool') ? $this->input->post('mSchool') : null;
		$data['edu_master_major'] = $this->input->post('mMajor') ? $this->input->post('mMajor') : null;
		$data['edu_master_student_id'] = $this->input->post('mID') ? $this->input->post('mID') : null;
		$data['edu_master_GPA'] = $this->input->post('mGPA') ? $this->input->post('mGPA') : null;
		
		$data['edu_doctor_university'] = $this->input->post('dSchool') ? $this->input->post('dSchool') : null;
		$data['edu_doctor_major'] = $this->input->post('dMajor') ? $this->input->post('dMajor') : null;
		$data['edu_doctor_student_id'] = $this->input->post('dID') ? $this->input->post('dID') : null;
		$data['edu_doctor_GPA'] = $this->input->post('dGPA') ? $this->input->post('dGPA') : null;
		$data['source'] = 'backend';
		
		$user = array();
		$user['username'] = $data['username'];
        $user['password'] = $data['password'];
        $user['profile'] = 'Tutor';
        $this->load->model('UserModel');
        
		if($this->input->post('status') == 'add') {
			$result = $this->UserModel->getUserByUsername($user['username']);
			if($result == null) {
				$id = $this->TutorModel->add($data);
				$user['create_by'] = $this->session->userdata('username');
				$this->UserModel->addUser($user);	
			} else {
				$data['username'] = null;
				$data['password'] = null;
				$id = $this->TutorModel->add($data);
				
				// reject
				$isRejectUser = true;
			}
		} else {
			$id = $this->input->post('tutorId');
			$tmp = $this->TutorModel->getByID($id);
			$data['tutor_id'] = $id;
			
			if($data['username'] == $tmp['username']) {
				$this->TutorModel->updateInfo($data);
				if($data['password'] != null) {
					$user['update_by'] = $this->session->userdata('username');
					$this->UserModel->updateUser($user);
				}	
			} else {
				$result = $this->UserModel->getUserByUsername($user['username']);
				if($result == null) {
					$this->TutorModel->updateInfo($data);
					$user['update_by'] = $this->session->userdata('username');
					$this->UserModel->deleteUser($tmp['username']);
					$this->UserModel->addUser($user);
				} else {
					$data['username'] = null;
					$data['password'] = null;
					$this->TutorModel->updateInfo($data);
					
					// reject
					$isRejectUser = true;
				}
			}
		}
		$sc = $this->input->post('chkSchool');
		$this->TutorSchoolModel->delete($id);
		if($sc) {
			foreach ($sc as $s) {
				$scArray = array('tutor_id' => $id, 'shool_id' => $s);
				$this->TutorSchoolModel->add($scArray);
			}
		}
		
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		// save profile image
		if(isset($_FILES) && @$_FILES['txtTutorImg']['error'] == '0') {
			$config['upload_path'] = './data-image/tutor/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-profile';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtTutorImg'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'tutor_id' => $id, 
	               'tutor_image' => $data['upload_data']['file_name']   
	            );
				$this->TutorModel->updateTutorImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		if(isset($isRejectUser) && $isRejectUser) {
			$this->msg = "Username ที่กำหนดถูกใช้แล้ว กรุณาเปลี่ยน username ใหม่";
		}		
		$this->detail($id);
	}
	
	function detail($tID)
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
		$this->load->model('TutorSchoolModel');
		if(isset($this->id) == false || $this->id == null) {
			$id = $tID;
		} else {
			redirect('login/index');
		}
		$data = $this->TutorModel->getByID($id);
		$this->load->model('SchoolModel');
		$data['schoolList'] = $this->SchoolModel->getAll();
		
		$this->load->model('empModel');
	    $emData = $this->empModel->getPermissionByUsername($this->session->userdata('username'));
	    if($this->session->userdata('profile') == 'Administrator' ||$emData['tutor_information'] == 'Y') {
	    	
		$data['birthday'] = $data['birthday'] == null ? '' : strftime("%d/%m/%Y",strtotime($data['birthday'])); 
		$data['user_expire'] = $data['user_expire'] == null ? '' : strftime("%d/%m/%Y",strtotime($data['user_expire']));
		$data['form_status'] = 'detail';
		$dataStuSc = $this->TutorSchoolModel->getByID($id);
		$StuSchoolChk = array();
		if($dataStuSc) {
			$i = 0;
			foreach ($dataStuSc as $s) {
				$StuSchoolChk[$i] = $s->shool_id;
				$i++;
			}
		}
		$data['schoolListCheck'] = $StuSchoolChk; 
		
		$this->load->view('header');
		$this->load->view('tutor-info-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	    } else {
	    	$msg['msg'] = 'คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลส่วนนี้';
			$data['form_status'] = 'notPermit';
	    	$this->load->view('header');
	    	$this->load->view('tutor-info-form', $data);
	    	$this->load->view('footer', $msg);
	    }
	}
	
	function detailContractInfo($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    $data = $this->TutorModel->getByID($tID);
	    if($data == null) {
	    	$this->index();
	    }
	    $this->load->model('empModel');
	    $emData = $this->empModel->getPermissionByUsername($this->session->userdata('username'));
	    if($this->session->userdata('profile') == 'Administrator' ||$emData['tutor_contract'] == 'Y') {
	   $data['form_status'] = 'xxx';
	    $this->load->view('header');
		$this->load->view('tutor-info-address-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	    } else {
	    	$msg['msg'] = 'คุณไม่มีสิทธิ์ในการเข้าถึงข้อมูลส่วนนี้';
			$data['form_status'] = 'notPermit';
	    	$this->load->view('header');
	    	$this->load->view('tutor-info-address-form', $data);
	    	$this->load->view('footer', $msg);
	    }
	}
	
	function detailTutorlandExp($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    $this->load->model('SchoolModel');
		$this->load->model('TutorHistoryModel');
	    
	    $data = $this->TutorModel->getByID($tID);
	    $data['schoolList'] = $this->SchoolModel->getAll();
	    $data['historyList'] = $this->TutorHistoryModel->getByID($tID);
	    $this->load->view('header');
		$this->load->view('tutor-info-job-history-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}
	
	function detailExp($tID) {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    
	    $data = $this->TutorModel->getByID($tID);
	    if($data == null) {
	    	$this->index();
	    }
	    $this->load->view('header');
		$this->load->view('tutor-info-job-profile-form', $data);
		if($this->msg != null && count($this->msg) > 0)
			$msg = array('msg' => $this->msg);
		else
			$msg = null;
		$this->load->view('footer', $msg);
	}
	
	function saveContractInfo() {
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    $id = $this->input->post('tutorId');
	    $data = array();
		$data['tutor_id'] = $id;
	    $data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;    
	    $data['telephone_home'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['mobile01'] = $this->input->post('txtMobile01') ? $this->input->post('txtMobile01') : null;
	    
		$netMobile = $this->input->post('selNetMobile01') ? $this->input->post('selNetMobile01') : null;
		$netMobileOther = $this->input->post('txtNetMobile01') ? $this->input->post('txtNetMobile01') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile01_network'] = $netMobile;
		} else {
			$data['mobile01_network'] = $netMobileOther;
		}
	    
		$data['mobile02'] = $this->input->post('txtMobile02') ? $this->input->post('txtMobile02') : null;
		$netMobile = $this->input->post('selNetMobile02') ? $this->input->post('selNetMobile02') : null;
		$netMobileOther = $this->input->post('txtNetMobile02') ? $this->input->post('txtNetMobile02') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile02_network'] = $netMobile;
		} else {
			$data['mobile02_network'] = $netMobileOther;
		}
		
	    $data['add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
	    $data['add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
	    $data['add_building_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
	    $data['add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
	    $data['add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
	    $data['add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
	    $data['add_distinct'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
	    $data['add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
	    $data['add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
	    $data['add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
	    $data['add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
	    
		$this->TutorModel->updateTutorAddress($data);
		
		// save map 
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		if(isset($_FILES) && @$_FILES['txtMap']['error'] == '0') {
			$config['upload_path'] = './data-image/tutor/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-addMap';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtMap'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'tutor_id' => $id, 
	               'add_map' => $data['upload_data']['file_name']   
	            );
				$this->TutorModel->updateTutorAddImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
	    $this->detailContractInfo($id);
	}
	
	function saveHistory()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->model('TutorHistoryModel');
	    $id = $this->input->post('tutorId');
	    
	    $data = array();
		$data['tutor_id'] = $id;
		$data['school_id'] = $this->input->post('selSchool') ? $this->input->post('selSchool') : null;
		$data['student_name'] = $this->input->post('txtStudent') ? $this->input->post('txtStudent') : null;
		$data['subject'] = $this->input->post('subject') ? $this->input->post('subject') : null;
		$data['class'] = $this->input->post('txtLevel') ? $this->input->post('txtLevel') : null;
		$data['teach_date'] = $this->input->post('txtDatePicker') ? $this->input->post('txtDatePicker') : null;
		$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		
		$this->TutorHistoryModel->add($data);
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		$this->detailTutorlandExp($id);
	}
	
	function saveProfile() 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
		$this->load->model('TutorModel');
	    $id = $this->input->post('tutorId');
	    
	    $data = array();
		$data['tutor_id'] = $id;
		
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		if(isset($_FILES) && @$_FILES['txtResume']['error'] == '0') {
			$config['upload_path'] = './data-image/tutor/';
			$config['allowed_types'] = 'gif|jpg|png|pdf|zip|doc|tif';
			$config['max_size']	= '5048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-resume';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtResume'))
			{
				$this->msg = "ไม่สามารถ upload ไฟล์ได้";			
			}
			else
			{
				$dataUpload = array('upload_data' => $this->upload->data());
				$data['hist_resume_path'] = $dataUpload['upload_data']['file_name'];
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		  
		$data['teach01_subject'] = $this->input->post('txtTeach01Subject') ? $this->input->post('txtTeach01Subject') : null;
		$data['teach01_class'] = $this->input->post('txtTeach01') ? $this->input->post('txtTeach01') : null;
		$data['teach01_inter'] = $this->input->post('txtTeach01Inter') ? $this->input->post('txtTeach01Inter') : null;
		$data['teach02_subject'] = $this->input->post('txtTeach02Subject') ? $this->input->post('txtTeach02Subject') : null;
		$data['teach02_class'] = $this->input->post('txtTeach02') ? $this->input->post('txtTeach02') : null;
		$data['teach02_inter'] = $this->input->post('txtTeach02Inter') ? $this->input->post('txtTeach02Inter') : null;
		$data['teach03_subject'] = $this->input->post('txtTeach03Subject') ? $this->input->post('txtTeach03Subject') : null;
		$data['teach03_class'] = $this->input->post('txtTeach03') ? $this->input->post('txtTeach03') : null;
		$data['teach03_inter'] = $this->input->post('txtTeach03Inter') ? $this->input->post('txtTeach03Inter') : null;
		$data['teach04_subject'] = $this->input->post('txtTeach04Subject') ? $this->input->post('txtTeach04Subject') : null;
		$data['teach04_class'] = $this->input->post('txtTeach04') ? $this->input->post('txtTeach04') : null;
		$data['teach04_inter'] = $this->input->post('txtTeach04Inter') ? $this->input->post('txtTeach04Inter') : null;
		$data['teach05_subject'] = $this->input->post('txtTeach05Subject') ? $this->input->post('txtTeach05Subject') : null;
		$data['teach05_class'] = $this->input->post('txtTeach05') ? $this->input->post('txtTeach05') : null;
		$data['teach05_inter'] = $this->input->post('txtTeach05Inter') ? $this->input->post('txtTeach05Inter') : null;
		$data['teach06_subject'] = $this->input->post('txtTeach06Subject') ? $this->input->post('txtTeach06Subject') : null;
		$data['teach06_class'] = $this->input->post('txtTeach06') ? $this->input->post('txtTeach06') : null;
		$data['teach06_inter'] = $this->input->post('txtTeach06Inter') ? $this->input->post('txtTeach06Inter') : null;
		
		$data['teach_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
		
		$data['hist_profile'] = $this->input->post('txtExp') ? $this->input->post('txtExp') : null;
		$data['hist_related_work'] = $this->input->post('txtExpMajor') ? $this->input->post('txtExpMajor') : null;
		$data['hist_note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		
		$sub = $this->input->post('selKnown') ? $this->input->post('selKnown') : null;
		$subOter = $this->input->post('txtKnownOther') ? $this->input->post('txtKnownOther') : null;
		if($sub != 'มีผู้แนะนำ') {
			$data['known_from'] = $sub;
		} else {
			$data['known_from'] = $subOter;
		}
		
		$otherArr = $this->input->post('subOth');
		$data['teach_other_1'] = $otherArr[0] ? $otherArr[0] : null;
		$data['teach_other_2'] = $otherArr[1] ? $otherArr[1] : null;
		$data['teach_other_3'] = $otherArr[2] ? $otherArr[2] : null;
		$data['teach_other_4'] = $otherArr[3] ? $otherArr[3] : null;
		$data['teach_other_5'] = $otherArr[4] ? $otherArr[4] : null;
		$data['teach_other_6'] = $otherArr[5] ? $otherArr[5] : null;
		
		$this->TutorModel->updateProfile($data);
		$this->msg = "บันทึกข้อมูลเรียบร้อย";
		$this->detailExp($id);
	}
	
	function download($tid) 
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    $this->load->model('TutorModel');
	    
	    $data = $this->TutorModel->getByID($tid);
	    
		$this->load->helper('download');
		$data = file_get_contents("data-image/tutor/".$data['hist_resume_path']); 
		$name = $data['hist_resume_path'];
		
		force_download($name, $data); 

	}
	
	
}

?>